//
//  ZYReadViewController.m
//  MusicMV
//
//  Created by Eternity on 16/3/17.
//  Copyright © 2016年 Eternity. All rights reserved.
//

#import "ZYReadViewController.h"

@interface ZYReadViewController ()<UITableViewDataSource, UITableViewDelegate, ZYPCollectionViewCellDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *mArr; /// 装满model

@end

@implementation ZYReadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self tabel];
    [self create];
}

- (void)viewWillAppear:(BOOL)animated {
    [_tableView reloadData];
}

- (void)tabel {
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-music.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:(UITableViewStylePlain)];
    _tableView.backgroundColor = [UIColor blackColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[ZYReadTableViewCell class] forCellReuseIdentifier:@"ZYReadTableViewCell"];
    // 刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self create];
    }];
    
   self.tableView.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
       
   }];

}
- (void)itemAction:(UIBarButtonItem *)item {
    
    ZYViewController *vc = [[ZYViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}


#pragma mark - item个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _mArr.count;
    
}
#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    ZYReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYReadTableViewCell"];
    ZYReadTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[ZYReadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ZYReadTableViewCell"];
    }
    cell.delegate = self;
    cell.model = _mArr[indexPath.row];
    return cell;
    
}



#pragma mark - row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYReadModel *model = _mArr[indexPath.row];
    CGFloat picweight = self.view.frame.size.width;
    CGFloat picheight = [model.height floatValue] / [model.width floatValue] * picweight;
    return picheight + 200 ;
    
}
// 协议
- (void)ZYPCollectionViewCellDelegate:(ZYReadModel *)model {
    ZYFirstViewController *vc = [[ZYFirstViewController alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    
}


- (void)create {
    NSDictionary *dic = @{@"next_key":@0, @"stype":@2, @"uid":@0};
    
//    NSDictionary *dic2 = @{@"Content-Type":@"application/x-www-form-urlencoded"};
//    
//    [XRAFNetworkTool POSTUrl:@"http://api.enjoymusicapp.com/api/v1.1/song/detail.ema?sign=087f4d56c24e006216bfd8caa6ee2e91&uid=386600&version=2.3.2&platform=ios" body:@"id=1428671&uid=386600" requestBodyStyle:XRRequestString headFile:dic2 response:XRJSON success:^(NSURLSessionDataTask *task, id responseObject) {
//        NSLog(@"%@", responseObject);
//    } failure:^(NSURLSessionDataTask *task, NSError *error) {
//        
//    }];
    
    [APPTools POSTWithURL:@"http://api.enjoymusicapp.com/api/v2.0/feed/hot.ema?sign=c9154741f99bfe97da1a4d7f408cac55" par:dic success:^(id responseObject) {
        NSLog(@"%@", responseObject);
        NSDictionary *dic = responseObject[@"data"];
        NSArray *arr = dic[@"items"];
        _mArr = [ZYReadModel modelWithArray:arr];
        [_tableView reloadData];
        // 停止
        [_tableView.mj_header endRefreshing];
    } filed:^(NSError *error) {
    
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
